module beeper_ctrl #(
	parameter 		MAX_5s = 6000_0000
)

	(
	input				clk,
	input 				rst,
    input       [5:0] 	diff_hour		,    	// 璁＄畻鏃堕棿宸細灏忔椂
    input       [5:0] 	diff_min 		,    	// 璁＄畻鏃堕棿宸細鍒嗛挓
    input       [5:0] 	diff_sec 		,   	// 璁＄畻鏃堕棿宸細绉� 
	input				key2_pulse      ,
	input		[3:0]	oled_mode       ,
	output reg  		beeper_start	,
	output reg			beeper_stop		,
	output reg			beeper_switch	,
	output reg			beeper_en
    );
	


	reg		[31:0]		cnt_5s;

	always@(posedge clk or negedge rst)
		if(!rst)
			cnt_5s <= 0;
		else if(beeper_en)
			cnt_5s <= cnt_5s + 1'b1;
		else
			cnt_5s <= 0;


	always@(posedge clk or negedge rst)
		if(!rst)
			beeper_start <= 1'b0;
		else if(oled_mode == 0 && diff_hour == 0 && diff_min == 0 && diff_sec == 0)
			beeper_start <= 1'b1;
		else
			beeper_start <= 1'b0;
	
	
	
	always@(posedge clk or negedge rst)
		if(!rst)
			beeper_stop <= 1'b0;
		else if(oled_mode == 0)
			beeper_stop <= key2_pulse;
		else
			beeper_stop <= 1'b0;
	
		
	
	always@(posedge clk or negedge rst)
		if(!rst)
			beeper_switch <= 1'b0;
		else if(oled_mode == 7)
			beeper_switch <= key2_pulse;
		else
			beeper_switch <= 1'b0;
	
	always@(posedge clk or negedge rst)
		if(!rst)
			beeper_en <= 1'b0;
		else if(beeper_start)
			beeper_en <= 1'b1;
		else if(beeper_stop)
			beeper_en <= 1'b0;
		else if(cnt_5s == MAX_5s - 1'b1)
			beeper_en <= 1'b0;
		else
			beeper_en <= beeper_en;
	
endmodule
